<script setup>
import { ref } from 'vue'
import { useVModel } from '@vueuse/core'
import { QuestionFilled, Loading } from '@element-plus/icons-vue'
import { deviceApi } from '@/api'
import { delay } from '@/utils/delay'

const props = defineProps({
  modelValue: Boolean,
  detail: Object
})
const emits = defineEmits(['close', 'update:modelValue'])
const dialogVisible = useVModel(props, 'modelValue', emits)

const ruleForm = ref({
  duty: 50,
  engine: 0
})
const rules = {
  engine: {
    required: true,
    message: '请选择舵机开关',
    trigger: 'change'
  },

  duty: [
    {
      required: true,
      message: '请输入占空比',
      trigger: 'blur'
    },
    {
      type: 'number',
      message: '占空比必须为数字值',
      trigger: 'blur'
    },
    {
      validator: (rule, value) => {
        if (value < 0 || value > 100) {
          return '占空比必须在0-100之间'
        }
        return true
      }
    }
  ]
}

const ruleFormRef = ref()

const handleOpen = () => {
  ruleFormRef.value.resetFields()

  if (props.detail) {
    ruleForm.value.engine = props.detail.engine
    ruleForm.value.duty = Number(props.detail.duty) || 0
  }
}

const switchLoading = ref(false)
const dutyLoading = ref(false)

async function handleSwitchChange(value) {
  switchLoading.value = true

  await deviceApi
    .steersMan({
      id: props.detail.id, // 设备id
      PWM: value // PWM状态：0 关闭 pwm输出；1：开启pwm输出
    })
    .finally(() => {
      switchLoading.value = false
    })
}

async function handleDutyChange(value) {
  dutyLoading.value = true
  await deviceApi
    .control({
      id: props.detail.id, // 设备id
      duty: value
    })
    .finally(() => {
      dutyLoading.value = false
    })
}

const delayHandleDutyChange = delay(handleDutyChange, 500)

const handleClose = () => {
  emits('close')
}
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    width="768px"
    title="舵机设置"
    v-bind="$attrs"
    @open="handleOpen"
    @close="handleClose"
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      label-position="right"
      label-suffix="："
    >
      <el-form-item label="舵机开关" prop="engine">
        <el-switch
          v-model="ruleForm.engine"
          :active-value="1"
          :inactive-value="0"
          :disabled="switchLoading"
          @change="handleSwitchChange"
        />

        <Loading
          v-if="switchLoading"
          class="ml-2 w-4 animate-spin opacity-65"
        />
      </el-form-item>

      <el-form-item v-if="ruleForm.engine" prop="duty">
        <template #label>
          <el-tooltip
            effect="dark"
            content="百分比, 0-100"
            placement="top-start"
          >
            <div class="flex items-center gap-[2px]">
              占空比
              <el-icon><QuestionFilled /></el-icon> ：
            </div>
          </el-tooltip>
        </template>

        <el-input-number
          v-model="ruleForm.duty"
          :min="0"
          :max="100"
          :disabled="switchLoading || dutyLoading"
          controls-position="right"
          @change="delayHandleDutyChange"
        />

        <Loading v-if="dutyLoading" class="ml-2 w-4 animate-spin opacity-65" />
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
